package com.uplooking.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.uplooking.constant.CommConstants;
import com.uplooking.constant.RedisConstants;
import com.uplooking.pojo.CustVO;
import com.uplooking.pojo.ShopVO;
import com.uplooking.service.UserService;
import com.uplooking.util.FileUtils;

@Controller
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@GetMapping(value="/home")
	public String home() {
		return "home";
	}
	
	@GetMapping(value="/regist/cust")
	public String cust1() {
		return "cust/regist";
	}
	
	@PostMapping(value="/regist/cust")
	public String cust2(String username,String password,Model model) throws Exception {
		model.addAttribute(CommConstants.CommMessage, 
				userService.registCust(username, password).get(CommConstants.CommMessage));
		return "cust/regist";
	}
	
	@GetMapping(value="/regist/shop")
	public String shop1(Model model) {
		model.addAttribute(CommConstants.CommDicts, userService.getShopValues());
		return "shop/regist";
	}
	
	@PostMapping(value="/regist/shop")
	public String shop2(String username,String password,ShopVO shopVO, MultipartFile logo,
			MultipartFile person, MultipartFile license,Model model) throws Exception {
		model.addAttribute(CommConstants.CommDicts, userService.getShopValues());
		model.addAttribute(CommConstants.CommMessage, 
				userService.registShop(username, password, shopVO, logo, person, license).get(CommConstants.CommMessage));
		return "shop/regist";
	}
	
	@GetMapping(value="/login")
	public String login() {
		return "login";
	}
	
	@Autowired
	StringRedisTemplate redis;
	
	@GetMapping(value="/photo/{username}")
	public void photo(@PathVariable(name="username")String username,
			HttpServletResponse resp) throws Exception {
		String text = redis.opsForHash().get(RedisConstants.PrefixImages, 
				RedisConstants.PrefixImagesPhoto+username).toString();
		FileUtils.decode(text, resp.getOutputStream());
	}
	
	@GetMapping(value="/logo/{username}")
	public void logo(@PathVariable(name="username")String username,
			HttpServletResponse resp) throws Exception {
		String text = redis.opsForHash().get(RedisConstants.PrefixImages, 
				RedisConstants.PrefixImagesLogo+username).toString();
		FileUtils.decode(text, resp.getOutputStream());
	}
	
	@GetMapping(value="/person/{username}")
	public void person(@PathVariable(name="username")String username,
			HttpServletResponse resp) throws Exception {
		String text = redis.opsForHash().get(RedisConstants.PrefixImages, 
				RedisConstants.PrefixImagesPerson+username).toString();
		FileUtils.decode(text, resp.getOutputStream());
	}
	
	@GetMapping(value="/license/{username}")
	public void license(@PathVariable(name="username")String username,
			HttpServletResponse resp) throws Exception {
		String text = redis.opsForHash().get(RedisConstants.PrefixImages, 
				RedisConstants.PrefixImagesLicense+username).toString();
		FileUtils.decode(text, resp.getOutputStream());
	}
	
	@GetMapping(value="/picture/{gno}")
	public void picture(@PathVariable(name="gno")String gno,
			HttpServletResponse resp) throws Exception {
		String text = redis.opsForHash().get(RedisConstants.PrefixImages, 
				RedisConstants.PrefixImagesPicture+gno).toString();
		FileUtils.decode(text, resp.getOutputStream());
	}
	
	@GetMapping(value="/index")
	public String index() {
		return "index";
	}
	
	@ResponseBody
	@GetMapping(value="/menu",produces={"application/json;charset=utf-8"})
	public String menu() {
		return JSONObject.toJSONString(userService.getMenuTree());
	}
	
	
	@PreAuthorize(value="hasRole('ROLE_ADMIN')")
	@GetMapping(value="/admin/user")
	public String userList() {
		return "admin/user/list";
	}
	
	@PreAuthorize(value="hasRole('ROLE_ADMIN')")
	@GetMapping(value="/admin/role")
	public String roleList() {
		return "admin/role/list";
	}
	
	@PreAuthorize(value="hasRole('ROLE_ADMIN')")
	@GetMapping(value="/admin/menu")
	public String menuList() {
		return "admin/menu/list";
	}
	
	@PreAuthorize(value="hasRole('ROLE_ADMIN')")
	@GetMapping(value="/admin/perm")
	public String permList() {
		return "admin/perm/list";
	}
	
	@PreAuthorize(value="hasRole('ROLE_SHOP')")
	@GetMapping(value="/shop/info")
	public String infoList() {
		return "shop/info/list";
	}
	
	@PreAuthorize(value="hasRole('ROLE_SHOP')")
	@GetMapping(value="/shop/good")
	public String goodList() {
		return "shop/good/list";
	}
	
	@PreAuthorize(value="hasRole('ROLE_SHOP')")
	@GetMapping(value="/shop/order")
	public String orderList() {
		return "shop/order/list";
	}
	
	
	@PreAuthorize(value="hasRole('ROLE_USER')")
	@GetMapping(value="/user/info")
	public String infoList1(Model model) {
		model.addAttribute(CommConstants.CommDicts, userService.getCustGenders());
		return "cust/info/list";
	}
	
	@PreAuthorize(value="hasRole('ROLE_USER')")
	@PostMapping(value="/user/info")
	public String infoList2(CustVO custVO,Model model) throws Exception {
		model.addAttribute(CommConstants.CommDicts, userService.getCustGenders());
		model.addAttribute(CommConstants.CommMessage, 
				userService.updateCust(custVO).get(CommConstants.CommMessage));
		return "cust/info/list";
	}
	
	@PreAuthorize(value="hasRole('ROLE_USER')")
	@GetMapping(value="/user/shopcar")
	public String shopCarList() {
		return "cust/shopcar/list";
	}
	
	@PreAuthorize(value="hasRole('ROLE_USER')")
	@GetMapping(value="/user/addr")
	public String addrList() {
		return "cust/addr/list";
	}
	
	@PreAuthorize(value="hasRole('ROLE_USER')")
	@GetMapping(value="/user/order")
	public String orderList1() {
		return "cust/order/list";
	}
	
}
